﻿using ERPPublic;
using MediatR;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using ProductManagement.Applcation.Command;
using ProductManagement.Domain.Dtos;

namespace ProductManagement.Controllers
{
    /// <summary>
    /// 消息管理
    /// </summary>
    [Route("api/[controller]/[action]")]
    [Authorize]
    [ApiController]
    public class MessageController : ControllerBase
    {
        public IMediator mediator;
        public MessageController(IMediator mediator)
        {
            this.mediator = mediator;
        }
        /// <summary>
        /// 新增消息表
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        /// 
        [HttpPost]
        public async Task<ApiResult<int>> SendMessage(MessageCreateDtos command)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {
                result.Data = await mediator.Send(new MessageCreateCommand { dtos = command });
            }
            catch (Exception)
            {
                result.Code = 500;
                result.Message = "Failed to send message";
                throw;
            }
            return result;
        }

        /// <summary>
        /// 显示消息表
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        /// 
        [HttpGet]
        public async Task<ApiResult<MessageSelectDtos>> SendMessageSelect([FromQuery]MessageSelectCommand commd)
        {
            ApiResult<MessageSelectDtos> result = new ApiResult<MessageSelectDtos>();
            try
            {
                result.Data = await mediator.Send(commd);
            }
            catch (Exception)
            {
                result.Code = 500;
                result.Message = "Failed to send message";
                throw;
            }
            return result;
        }
    }
}
